using System;
using System.Web;

namespace Backfire
{
  /// <summary>
  /// Main API object
  /// </summary>
  public class HQ
  {
    /// <summary>
    /// Sends down a message to the client, so that the Backfire javascript can act upon it.
    /// </summary>
    /// <param name="message">Message to send.</param>
    /// <param name="response">The current HttpResponse object</param>
    public static void SendMessageToClient(PostbackMessage message, HttpResponse response) 
    {
      response.Write(String.Format("<script>window.parent.postMessage(\"{0}\", '*')</script>", message));
    }

    /// <summary>
    /// Processes the posted changes by going through them sequentially. It builds up a
    /// dictionary of uri/css-contents key/value pairs. When needed it performs a call
    /// to cssLoader in order to retrieve the original CSS. When all changes are processed,
    /// it will make the appropriate calls to the cssSaver.
    /// </summary>
    /// <param name="changes">The changes posted by the BackFire.js library</param>
    /// <param name="cssLoader">Delegate to resolve css given a uri</param>
    /// <param name="cssSaver">Delegate to save the final CSS with</param>
    /// <returns>A success or fail message.</returns>
    public static PostbackMessage Process(string changes, LoadCSS cssLoader, SaveCSS cssSaver)
    {
      return new PostProcessor().Process(changes, cssLoader, cssSaver);
    }
  }
}